/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.commands;

import org.jfonia.connect5.intervals.Interval;
import org.jfonia.connect5.intervals.LinkedDoubleIntervalList;
import org.jfonia.connect5.intervals.LinkedIntIntervalList;
import org.jfonia.connect5.intervals.LinkedLongIntervalList;

/**
 *
 * @author Jannes Plyson
 */
public class AddInterval implements ICommand{
    private Interval interval;
    private Object list;

    public AddInterval(Interval interval, LinkedIntIntervalList list){
        this.interval = interval;
        this.list = list;
    }

    public AddInterval(Interval interval, LinkedLongIntervalList list){
        this.interval = interval;
        this.list = list;
    }

    public AddInterval(Interval interval, LinkedDoubleIntervalList list){
        this.interval = interval;
        this.list = list;
    }

    public void execute() {
        if(list instanceof LinkedIntIntervalList){
            ((LinkedIntIntervalList)list).add(interval);
        }else if(list instanceof LinkedLongIntervalList){
            ((LinkedLongIntervalList)list).add(interval);
        }else{
            ((LinkedDoubleIntervalList)list).add(interval);
        }
    }

    public void undo() {
        int index;
        if(list instanceof LinkedIntIntervalList){
            index = ((LinkedIntIntervalList)list).indexOf(interval);
            ((LinkedIntIntervalList)list).remove(index);
        }else if(list instanceof LinkedLongIntervalList){
            index = ((LinkedLongIntervalList)list).indexOf(interval);
            ((LinkedLongIntervalList)list).remove(index);
        }else{
            index = ((LinkedDoubleIntervalList)list).indexOf(interval);
            ((LinkedDoubleIntervalList)list).remove(index);
        }
    }
}
